# -*- coding: utf-8 -*-
from django.views.generic import TemplateView, ListView
from .models import Species, Subspecies , Bird
from apps.home.models import UserProfile
from django.http import HttpResponse
from django.shortcuts import render_to_response

from apps.cart.models import Cart
from apps.cart import cart


class catalog(ListView):
    template_name = 'catalog/catalog_base.html'
    context_object_name = 'species'
    model = Species
    
class subspecies(TemplateView):
    template_name = 'catalog/subspecies.html'
     
    def get_context_data(self, **kwargs):
        context = super(subspecies, self).get_context_data(**kwargs)
        specieTemp = Species.objects.get(id=self.kwargs.get('id'))
        subspeciesTemp = Subspecies.objects.filter(species=self.kwargs.get('id'))
        #context['id'] = self.kwargs.get('id')
        if not subspeciesTemp:
            self.template_name = 'catalog/birds.html'
        else:
            self.template_name = 'catalog/subspecies.html'    
        context['Specie'] = specieTemp
        context['Subspecies'] = subspeciesTemp
        context['Birds'] = Bird.objects.filter(specie=self.kwargs.get('id'))
        return context
    
    
class show_birds(TemplateView):
    template_name = 'catalog/birds.html'
     
    def get_context_data(self, **kwargs):
        context = super(show_birds, self).get_context_data(**kwargs)
        specieTemp = Species.objects.get(id=self.kwargs.get('id'))
        subspecieTemp = Subspecies.objects.get(id=self.kwargs.get('idSub'))
           
        context['Specie'] = specieTemp
        context['Subspecie'] = subspecieTemp        
        context['Birds'] = Bird.objects.filter(specie=self.kwargs.get('id'), subspecie=self.kwargs.get('idSub'))
        return context   
    
    
class bird_detail(TemplateView):
    template_name = 'catalog/birdDetail.html'
     
    def get_context_data(self, **kwargs):
        context = super(bird_detail, self).get_context_data(**kwargs)
        specieTemp = Species.objects.get(id=self.kwargs.get('id'))
        subspecieTemp = Subspecies.objects.get(id=self.kwargs.get('idSub'))
        birdTemp = Bird.objects.get(id=self.kwargs.get('idBir'))        
        
        context['Specie'] = specieTemp
        context['Subspecie'] = subspecieTemp 
        context['Bird'] = birdTemp         

        if not subspecieTemp:       
            context['DetailBirds'] = Bird.objects.filter(specie=self.kwargs.get('id'), id=self.kwargs.get('idBir'))
        else:
            context['DetailBirds'] = Bird.objects.filter(specie=self.kwargs.get('id'), subspecie=self.kwargs.get('idSub'), id=self.kwargs.get('idBir'))
     
        return context   

      
class go_cart(TemplateView):
    template_name = 'catalog/cart.html'    # 
  
  
class show_item(TemplateView):
    
    def get(self, request , *args, **kwargs):
        id = request.GET['id']
        ave = Bird.objects.get(id = id)
        username = request.user.username
        user = UserProfile.objects.get(user__username = username)
        cart = Cart()
        cart.user = user
        cart.save()
        return HttpResponse("HOLA PRUEBA")
        
      
#*****************************CART**************************************++


def add_to_cart(request, product_id, quantity):
    product = Bird.objects.get(id=product_id)
    cart = Cart(request)
    cart.add(product, product.unit_price, quantity)

def remove_from_cart(request, product_id):
    product = Bird.objects.get(id=product_id)
    cart = Cart(request)
    cart.remove(product)

def get_cart(request):
    return render_to_response('cart.html', dict(cart=Cart(request)))         
    

#
# new product view, with POST vs GET detection
#def show_product(request, bird_slug, template_name="catalog/product.html"):
#    p = get_object_or_404(Bird, slug=bird_slug)
#    categories = p.categories.all()
#    page_title = p.name
#    # need to evaluate the HTTP method
#    if request.method == 'POST':
#        # add to cart…create the bound form
#        postdata = request.POST.copy()
#        form = BirdAddToCartForm(request, postdata)
#        #check if posted data is valid
#        if form.is_valid():
#            #add to cart and redirect to cart page
#            cart.add_to_cart(request)
#            # if test cookie worked, get rid of it
#            if request.session.test_cookie_worked():
#                request.session.delete_test_cookie()
#            url = urlresolvers.reverse('show_cart')
#            return HttpResponseRedirect(url)
#        else:
#            # it’s a GET, create the unbound form. Note request as a kwarg
#            form = BirdAddToCartForm(request=request, label_suffix=':')
        # assign the hidden input the product slug
#        form.fields['bird_slug'].widget.attrs['value'] = bird_slug
        # set the test cookie on our first GET request
#        request.session.set_test_cookie()
#        return render_to_response('catalog/product.html', locals(), 
#                                  context_instance=RequestContext(request))"""